package doc2db.interfaces;

import doc2db.Launcher;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 *
 * @author yura
 */
public class WaitFrame {
    private JFrame tempWait = new JFrame("doc2db - wait");
    private final int width = 250;
    private final int height = 150;
    
    public JFrame initWaitFrame() {
        tempWait.setSize(width, height);
        try {
            tempWait.setIconImage(Launcher.getImage("main.jpg"));
        } catch (Exception ex) {}
        JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        progressBar.setPreferredSize(new Dimension(230, 20));

        JPanel pane = new BGDemo(progressBar);
        tempWait.setContentPane(pane);

        tempWait.setLocationRelativeTo(null);
        tempWait.setMinimumSize(tempWait.getSize());
        tempWait.setResizable(false);
        tempWait.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        tempWait.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        tempWait.setUndecorated(true);
        tempWait.setAlwaysOnTop(true);
        tempWait.setVisible(true);
        return tempWait;
    }

    public class BGDemo extends JPanel {
        Image bgImage = null;
        JComponent aButton;
        JLabel l = new JLabel("Подождите пожалуйста");
        int x = (width - l.getPreferredSize().width) / 2;
        int y = (150 - l.getPreferredSize().height) / 2 - 22;
        int w = l.getPreferredSize().width;
        int h = l.getPreferredSize().height;

        public BGDemo(JComponent c) {
            super(null);
            aButton = c;
            getFileImage();
            l = new JLabel("Загрузка");
            l.setFont(new Font(Font.SERIF, Font.BOLD, 14));
            x = 6;
            y = (height - l.getPreferredSize().height) - 32;
            w = l.getPreferredSize().width;
            h = l.getPreferredSize().height;
            l.setBounds(x, y, w, h);
            aButton.setBounds(10, y + aButton.getPreferredSize().height, 230, 20);
            add(l);
            add(aButton);
        }

        @Override
        public void paintComponent(Graphics g) {
            if (bgImage != null) {
                Dimension d = getSize();
                int width = d.width;
                int height = d.height;
                g.drawImage(bgImage, 0, 0, width, height, this);
                //g.setColor(Color.white);
                //g.fillRect(x, y, w, h);
            } else {
            }
        }

        private void getFileImage()  {
            bgImage = Launcher.getImage("tempWait.jpg");
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(bgImage, 0);
            try {
                mt.waitForAll();
            } catch (InterruptedException ex) {
                System.err.println("waite frame - " + ex);
            }
        }
    }

}
